<?php
  include("header.php");
  include("functions.php");
  
  if ($_SESSION['user_level'] != 1) {
      header("location: redirect.php");
  } else {
      $pageTitle = "Registration";
	  
	  $allow_registration = get_value_of('$allow_registration');
      $allow_registration = trim($allow_registration);
	  $allow_registration = preg_replace("#[^0-9]#", "", $allow_registration);
	 if ($allow_registration==1)  {$ary='checked'; $arn='';}
     if ($allow_registration==0)  {$ary=''; $arn='checked';}
	  
      $max_username_char = get_value_of('$max_username_char');
      $max_username_char = trim($max_username_char);
	  $max_username_char = preg_replace("#[^0-9]#", "", $max_username_char);
	  
	  $min_username_char = get_value_of('$min_username_char');
      $min_username_char = trim($min_username_char);
	  $min_username_char = preg_replace("#[^0-9]#", "", $min_username_char);
	  
      $captcha = get_value_of('$captcha');
      $captcha = trim($captcha);
	  $captcha = preg_replace("#[^0-9]#", "", $captcha);
	 if ($captcha==1)  {$cay='checked'; $can='';}
     if ($captcha==0)  {$cay=''; $can='checked';}
	  
	  $flood_login_attempt = get_value_of('$flood_login_attempt');
      $flood_login_attempt = trim($flood_login_attempt);
	  $flood_login_attempt = preg_replace("#[^0-9]#", "", $flood_login_attempt);
	  
	  $flood_ban = get_value_of('$flood_ban');
      $flood_ban = trim($flood_ban);
	  $flood_ban = preg_replace("#[^0-9]#", "", $flood_ban);
	  
	  $email_activation = get_value_of('$email_activation');
      $email_activation = trim($email_activation);
	  $email_activation = preg_replace("#[^0-9]#", "", $email_activation);	

     if ($email_activation==1)  {$eayes='checked'; $eano='';}
     if ($email_activation==0)  {$eayes=''; $eano='checked';}
	 
      if ($_SERVER['REQUEST_METHOD'] != 'POST') {
          $tags = array('{TITLE}','{CONFIRM}','{ALLOW_REG}','{AR_Y}','{AR_N}','{MAX_UN}', '{UN_CHAR}','{CAPTCHA}','{CA_Y}','{CA_N}','{FLOOD}','{FLOOD_VALUE}','{FLOOD_BAN}','{FLOOD_BAN_VALUE}','{EMAIL_ACT}','{EA_Y}','{EA_N}','{MIN_UN}','{MIN_UN_VALUE}');
          $data = array('Login/Registration settings','Confirm','Allow user registration',$ary,$arn,'Max. username (char.)',$max_username_char,'Captcha on registration',$cay,$can,'Fail login attempt<br>(0 to disable)',$flood_login_attempt,'Ban time after fail logins (minutes)',$flood_ban,'User email activation',$eayes,$eano,'Min. username (char.)',$min_username_char);
          
          echo str_replace($tags, $data, file_get_contents("./style/" . $default_style . "/registration.html"));
      } else {
          write_value_of('$allow_registration', "$allow_registration", mysql_real_escape_string($_POST['allow_reg']));
          write_value_of('$max_username_char', "$max_username_char", mysql_real_escape_string($_POST['max_un_char']));
		  write_value_of('$min_username_char', "$min_username_char", $_POST['min_un_char']);
          write_value_of('$captcha', "$captcha", mysql_real_escape_string($_POST['captcha']));
		  write_value_of('$flood_login_attempt', "$flood_login_attempt", mysql_real_escape_string($_POST['flood']));
		  write_value_of('$flood_ban', "$flood_ban", mysql_real_escape_string($_POST['flood_ban']));
		  write_value_of('$email_activation', "$email_activation", mysql_real_escape_string($_POST['email_activation']));
          
          echo "<b>Information</b><hr><br /><div align='center'>Configuration updated successfully.<br /><br /><a href = 'registration.php'>Back to previous page</a></div>";
      }
      // Get all the page's HTML into a string
      $pageContents = ob_get_contents();
      // Wipe the buffer 
      ob_end_clean();
      echo str_replace('<!--TITLE-->', $pageTitle, $pageContents);
  }
?>
